(function() {
    'use strict';

    /** 预约项目类型管理
     * @ngdoc function
     * @name webApp.controller:GzhmenuCtrl
     * @description 
     * # GzhmenuCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('GzhmenuCtrl', GzhmenuCtrl);

    GzhmenuCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function GzhmenuCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            if ($scope.wxappid) {
                $scope.clickData = 0;
                dataService.wxCurrentSelfmenuGet($scope.wxappid).then(function(rest) {
                    if (rest.selfmenu_info && rest.is_menu_open) {
                        $scope.itemsList = rest.selfmenu_info.button;
                        $scope.HandleData();
                        // $scope.menuTop();
                        console.error($scope.itemsList);
                        $scope.Options();
                    }
                })
            }
        }

        //回调方法
        $scope.appletback = function(attach, clickData) {
            if (attach) {
                $scope.clickData.pagepath = attach.path;
                $scope.clickData.appid = attach.wxappid;
                $scope.clickData.nick_name = attach.wxappname;
                console.error("attach", attach);
                console.error("clickData", clickData);
            }
        }
        $scope.HandleData = function() {
            _.map($scope.itemsList, function(data, key) {
                if (data.sub_button) {
                   
                } else {
                    if(data.type =='img'){
                        data.type  ='media_id';
                        data.sub_click_type  ='img';
                        data.media_id  = data.value;
                        data.url  = $scope.wxMaterialGet(data.value);
                    }
                    
                    if(data.type =='news'){
                        data.type  ='media_id';
                        data.sub_click_type  ='news';
                    }
                }
            });
            console.error("$scope.itemsList", $scope.itemsList);
        }
        
       

        $scope.wxgzhcallback = function(wxappid) {
            console.error(wxappid);
            $scope.wxappid = wxappid;
            console.error($scope.wxappid);
            $scope.listAction();
        }

        $scope.clickBtn = function(id, data) {
            $timeout(function() {
                if (data) {
                    $scope.clickData = data;
                   
                    $scope.clickData.subtype = 1;
                    var title = $('#' + id + 'top').html();
                }
            });
        }

        $scope.addModular = function(hashKey, da) {
            if (!da.sub_button) {
                helper.myConfirm("添加子菜单后，一级菜单的内容将被清除。确定添加子菜单", function() {
                    $scope.addSubBtn(hashKey);
                })
            } else {
                $scope.addSubBtn(hashKey);
            }

        }
        $scope.addSubBtn = function(hashKey) {
            $timeout(function() {
                var data = {
                    name: "子菜单",
                };
                console.error("hashKey", hashKey);
                _.map($scope.itemsList, function(item, ikey) {
                    if (item.$$hashKey == hashKey) {
                        if (!item.sub_button) {
                            $scope.itemsList[ikey] = {
                                name: item.name,
                                sub_button: {},
                                $$hashKey: item.$$hashKey
                            }
                            console.error("item", item);
                            $scope.itemsList[ikey].sub_button.list = [];
                        }
                        $scope.itemsList[ikey].sub_button.list.push(data);
                        // $scope.menuTop();
                    }
                });
                $scope.clickData = data;
                $scope.clickData.subtype = 2;

                console.error("$scope.clickData", $scope.clickData, $scope.itemsList);
            });
        }
        $scope.clickModular = function(data) {
            $scope.clickData = data;
            $scope.clickData.subtype = 2;
            if (data.type !== 'view' && data.type !== 'miniprogram') {
                $scope.clickData.xiaoxitype = 'xiaoxi';
            }
        }

        //删除元素属性
        $scope.derBtn = function(clickData) {
            helper.myConfirm("删除后[" + clickData.name + "]菜单下设置的内容将被删除", function() {
                $timeout(function() {
                    _.map($scope.itemsList, function(da, key) {
                        if (clickData.subtype == 1) {
                            if (da.name == clickData.name && da.$$hashKey == clickData.$$hashKey) {
                                $scope.itemsList.splice(key, 1);
                            }
                        } else {
                            //子订单
                            if (da.sub_button) {
                                _.map(da.sub_button.list, function(dasvb, keysub) {
                                    console.error("dasvb", dasvb);
                                    if (dasvb.name == clickData.name && dasvb.$$hashKey == clickData.$$hashKey) {
                                        $scope.itemsList[key].sub_button.list.splice(keysub, 1);
                                        if (!$scope.itemsList[key].sub_button.list.length) {
                                            $scope.itemsList[key] = {
                                                name: $scope.itemsList[key].name,
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    });
                    $scope.clickData = 0;
                });
            })
        }

        $scope.addBtn = function() {
            if ($scope.itemsList.length < 3) {
                var data = {
                    name: "菜单名称",
                };
                $scope.clickData = data;
                $scope.clickData.subtype = 1;
                $scope.itemsList.push(data);
                console.error("$scope.clickData", $scope.clickData);
            }
        }
        //
        $scope.derNews = function() {
            if ($scope.clickData.news_info) {
                delete $scope.clickData.news_info;
                delete $scope.clickData.value;
            }
        }

        $scope.addimgurl = function(attach) {
            if (attach) {
                $scope.clickData.url = attach.url;
                $scope.clickData.value = attach.media_id;
                $scope.clickData.media_id = attach.media_id;
            }
        }
        $scope.saveMenu = function() {
            var data = clone($scope.itemsList);
            var newsData = { button: [] };

            _.map(data, function(da, key) {
                if (da.subtype) {
                    delete da.subtype;
                }
                if (da.sucai) {
                    delete da.sucai;
                }
                if (da.num) {
                    delete da.num;
                }
                if (da.type == 'view') {
                    if (da.appid) {
                        delete da.appid;
                    }
                    if (da.pagepath) {
                        delete da.pagepath;
                    }
                }
                newsData.button[key] = clone(da);
                if (da.sub_button) {
                    newsData.button[key].sub_button = [];
                    _.map(da.sub_button.list, function(da1, key1) {
                        if (da1.subtype) {
                            delete da1.subtype;
                        }
                        if (da1.sucai) {
                            delete da1.sucai;
                        }
                        if (da1.num) {
                            delete da1.num;
                        }
                        if (da1.key && da1.type !== 'click') {
                            delete da1.key;
                        }

                        if (da1.type == 'media_id' && 　da1.sub_click_type == 'news') {
                            da1.type = 'view_limited';
                            delete da1.url;
                        }


                        if (da1.sub_click_type) {
                            delete da1.sub_click_type;
                        }
                        if (da1.news_info) {
                            delete da1.news_info;
                        }
                        if (da1.xiaoxitype) {
                            delete da1.xiaoxitype;
                        }
                        if (da1.type == 'media_id') {
                            delete da1.url;
                        }
                        newsData.button[key].sub_button[key1] = clone(da1);
                    })
                }
            })

            console.error(newsData);
            dataService.wxCurrentSelfmenuCreate($scope.wxappid, newsData).then(function(rest) {
                if (rest.errmsg == 'ok') {
                    helper.myAlert("成功！");
                } else {
                    helper.myAlert("失败！" + rest.errcode + rest.errmsg);
                }
            })

        }
        //获取图片
        $scope.wxMaterialGet =function(media_id){
            
            var data ={
                authorizer_appid:$scope.wxappid,
                media_id:media_id,
            };
            console.error(data);
            dataService.wxMaterialGet(data).then(function(rest) {
                console.error("rest", rest);
            })
        }
        $scope.Options = function(pageNo) {
            //上下移动
            $scope.sortableCloneOptions = {
                containment: '#sortable-container',
                allowDuplicates: true,
                ctrlClone: true
            };
            //z左右移动
            $scope.sortableOptions = {
                containment: '#table-container',
                containerPositioning: 'relative'
            };

        }
        $scope.ballckData = function(data) {
            if (data) {
                $scope.clickData.media_id = data.thumb_media_id;
                $scope.clickData.news_info = $scope.clickData.news_info ? $scope.clickData.news_info : {};
                $scope.clickData.news_info.list = $scope.clickData.news_info.list ? $scope.clickData.news_info.list : [];
                $scope.clickData.news_info.list.push(data);
            }
        }
        $scope.ballckUrl = function(data) {
            console.error($scope.wxappid);
            if (data) {
                $scope.clickData.url = data.url;
                $scope.clickData.sucai = data.title;
            }
            // 对于不同的菜单类型，value的值意义不同。官网上设置的自定义菜单：
            //  Text:保存文字到value； 
            //  Img、voice：保存mediaID到value；
            //   Video：保存视频下载链接到value；
            //    News：保存图文消息到news_info，同时保存mediaID到value；
            //     View：保存链接到url。 
            //     使用API设置的自定义菜单： click、scancode_push、scancode_waitmsg、pic_sysphoto、pic_photo_or_album、 pic_weixin、location_select：保存值到key；view：保存链接到url
        }
        //图片
        $scope.subclick = function(type) {
            if (type) {
                $scope.clickData.sub_click_type = type;
            }
        }

        $scope.xiaoxiClick = function() {
            if ($scope.clickData.type == 'view' && $scope.clickData.type == 'miniprogram') {
                $scope.clickData.type = 'text';
            }
        }
    }
})();